package com.example.demo2.config;

import com.example.demo2.config.intercepor.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfigurer implements WebMvcConfigurer {

    @Autowired
    private LoginInterceptor loginInterceptor;

    // 这个方法用来注册拦截器，添加注册自定义拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // addPathPatterns("/**") 表示拦截所有的请求，
        // excludePathPatterns() 表示拦截白名单
        String[] addPathPatterns={
                "/**"
        };
        String[] excludePathPatterns={
                "/web/login.html",
                "/web/forgetPwd.html",
                "/web/register.html",
                "/bootstrap-4.6.1-dist/**",
                "/css/**",
                "/fonts/**",
                "/font-awesome-4.7.0/**",
                "/img/**",
                "/js/**",
                "/vendor/**",
                "/word/**",
                "/user/login",
                "/user/register",
                "/user/forgetPwd",
                "/web/forgetPwd2.html",
                "/web/error.html",
                "/",
                "/user/upDateUserPwd"
        };
        registry.addInterceptor(loginInterceptor).addPathPatterns(addPathPatterns).excludePathPatterns(excludePathPatterns);
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {

        //默认地址（可以是页面或后台请求接口）
        registry.addViewController("/").setViewName("forward:web/login.html");
        //设置过滤优先级最高
        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
    }
}